﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace SynoManager.DownloadStation
{
    public partial class AddDownloadTaskDialog : Form
    {
        public AddDownloadTaskDialog()
        {
            InitializeComponent();
        }

        private OpenDownloadFileDialog openDownloadFileDialog = null;
        public AddDownloadTaskDialog(DownloadStationService service)
            :this()
        {
            openDownloadFileDialog = new OpenDownloadFileDialog(service);
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            if (openDownloadFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                locationTextBox.Text = openDownloadFileDialog.FileName;
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (Validate(locationTextBox.Text))
            {
                Properties.Settings.Default.AddDownloadTaskDialogExpanded = (expandButton.Enabled == false) ;
                DialogResult = DialogResult.OK;
            }
            else
                MessageBox.Show("Please enter a location", "New Download Task", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private const string urlRegEx = "http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?";
        public bool Validate(string input)
        {
            if (string.IsNullOrEmpty(input))
                return false;

            if (!input.Contains(Environment.NewLine))
            {
                Uri tempUri;
                return Uri.TryCreate(input, UriKind.Absolute, out tempUri);
            }
            else
            {
                MatchCollection matches = GetUrls(input);
                return (matches.Count > 0);
            }
        }

        private MatchCollection GetUrls(string input)
        {
            Regex regex = new Regex(urlRegEx, RegexOptions.IgnoreCase | RegexOptions.Multiline);
            return regex.Matches(input);
        }

        public string[] Locations
        {
            get
            {
                if (locationTextBox.Multiline)
                {
                    var matches = GetUrls(locationTextBox.Text);
                    List<string> result = new List<string>();
                    foreach (Match match in matches)
                        result.Add(match.Value);
                    return result.ToArray();
                }
                else
                {
                    return new string[] {locationTextBox.Text};
                }
            }
        }

        private void expandButton_Click(object sender, EventArgs e)
        {
            ExpandWindow();
        }

        private void ExpandWindow()
        {
            locationTextBox.Multiline = true;
            FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            this.Height = 228;
            expandButton.Enabled = false;
            Properties.Settings.Default.AddDownloadTaskDialogExpanded = true;
        }

        private void AddDownloadTaskDialog_Load(object sender, EventArgs e)
        {
            string clipboard = Clipboard.GetText();
            if (!String.IsNullOrEmpty(clipboard))
            {
                if (Validate(clipboard))
                {
                    if (clipboard.Contains(Environment.NewLine))
                        ExpandWindow();
                    locationTextBox.Text = clipboard;
                }
            }

            if (Properties.Settings.Default.AddDownloadTaskDialogExpanded)
                ExpandWindow();
        }
    }
}
